const api = require("../../api/sop");
import { err, to, toast } from "../../utils/wxapi";
Page({
  /**
   * 页面的初始数据
   */
  data: {
    valueInput: "",
    // 判断点击
    navNumber: "1",
    // 声明变量来接岗位列表
    homeJobLists: [],
    page: 1,
    size: 20,
    reqCode: "",
    city: wx.getStorageSync("city") || "上海市",
    total: 0,
    nav: [
      { img: "../../images/home/home_nav1.png", title: "补贴推广", url: "" },
      {
        img: "../../images/home/home_nav2.png",
        title: "长期职位",
        url: "/two/pages/pluralistic/pluralistic?state=2",
      },
      {
        img: "../../images/home/home_nav3.png",
        title: "日结兼职",
        url: "/two/pages/pluralistic/pluralistic?state=1",
      },
      {
        img: "../../images/home/home_nav4.png",
        title: "地区招商",
        url: "/two/pages/attract/attract",
      },
    ],
  },
  onLoad() {},
  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    if (typeof this.getTabBar === "function" && this.getTabBar()) {
      this.getTabBar().setData({
        selected: 0,
      });
    }
    const data = JSON.stringify({});
    this.postSeach(data);
    this.getUserHome();
  },
  selectCity(event) {
    const selectedCity = event.detail.value[1]; // 假设城市选择后的值存放在
    this.setData({ city: selectedCity });
    wx.setStorageSync("city", selectedCity);
    const data = JSON.stringify({ interviewAddress: selectedCity });
    this.postSeach(data);
  },
  nav_click() {
    this.setData({ navNumber: "1", reqCode: "", homeJobLists: [] });
    const data = JSON.stringify({});
    this.postSeach(data);
  },
  nav_clicks() {
    this.setData({
      navNumber: "2",
      reqCode: "DATE_NEAR_TO_FAR",
      homeJobLists: [],
    });
    const { city } = this.data;
    const data = JSON.stringify({ interviewAddress: city });
    this.postSeach(data);
  },
  goToSearch() {
    to("/two/pages/serach/serach");
  },
  tonav(e) {
    const { url } = e.currentTarget.dataset;
    if (url) {
      to(url);
    } else {
      toast("功能暂未开放，敬请期待");
    }
  },
  // 岗位列表
  async postSeach(data) {
    try {
      const { page, size, reqCode, homeJobLists } = this.data;
      // const data = JSON.stringify({ interviewAddress: city });
      const res = await api.work_list({ data, page, size, reqCode });
      if (res.respCode === 200) {
        const Arrays = JSON.parse(res.source);
        console.log(Arrays);
        const total = res.pageTotal;
        const updatelist = page > 1 ? [...homeJobLists, ...Arrays] : Arrays;
        this.setData({ homeJobLists: updatelist, total });
      }
    } catch (error) {
      err(error);
      throw error;
    }
  },
  // 用户信息
  async getUserHome() {
    try {
      const res = await api.getUser();
      if (res.respCode === 200) {
        const user = JSON.parse(res.source);
        wx.setStorageSync("user", user);
        wx.setStorageSync("userId", user.userId);
        wx.setStorageSync("wxOpenId", user.wxOpenId);
      }
    } catch (error) {
      err(error);
      throw error;
    }
  },
  arraysEqual(arr1, arr2) {
    if (arr1.length !== arr2.length) {
      return false;
    }
    const sortedArr1 = arr1.slice().sort();
    const sortedArr2 = arr2.slice().sort();
    for (let i = 0; i < sortedArr1.length; i++) {
      if (sortedArr1[i] !== sortedArr2[i]) {
        return false;
      }
    }
    return true;
  },
  onReachBottom() {
    console.log(1);
    const { page, total, homeJobLists } = this.data;
    if (homeJobLists.length < total) {
      this.setData({ page: page + 1 });
      this.postSeach();
    } else {
      toast("暂无更多内容");
    }
  },
  onPullDownRefresh() {
    this.setData({ page: 1, navNumber: "1" });
    const data = JSON.stringify({});
    this.postSeach(data);
    wx.stopPullDownRefresh();
  },
  onShareAppMessage() {},
});
